package com.developmentReformProject.config;

import com.developmentReformProject.annotation.MyLog;
import com.developmentReformProject.dao.AdminDao;
import com.developmentReformProject.entity.SysLog;
import com.developmentReformProject.service.SysLogService;
import com.developmentReformProject.utils.HttpContextUtils;
import com.developmentReformProject.utils.IPUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

@Aspect
@Component
public class SysLogAspect {

    @Autowired
    private SysLogService sysLogService;
    @Autowired
    private AdminDao adminDao;
    //定义切点 @Pointcut
    //在注解的位置切入代码
    @Pointcut("@annotation( com.developmentReformProject.annotation.MyLog)")
    public void logPoinCut() {
    }

    //切面 配置通知
    @AfterReturning("logPoinCut()")
    public void saveSysLog(JoinPoint joinPoint) throws Exception {
        System.out.println("切面。。。。。");
        //保存日志
        SysLog sysLog = new SysLog();

        //从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入点所在的方法
        Method method = signature.getMethod();

        //获取操作
        MyLog myLog = method.getAnnotation(MyLog.class);
        if (myLog != null) {
            String value = myLog.value();
            sysLog.setOperation(value);//保存获取的操作
        }

        //获取请求的类名
        String className = joinPoint.getTarget().getClass().getName();
        //获取请求的方法名
        String methodName = method.getName();
        sysLog.setMethod(className + "." + methodName);

        //请求的参数
        Object[] args = joinPoint.getArgs();
      /**  String params=null;
        for(int i=0;i<args.length;i++){
            params += args[i].getClass();
        }
        //将参数所在的数组转换成json
        //String params = JSON.toJSONString(args);
        sysLog.setParams(params);*/
        //请求的时间
        Date date=new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sysLog.setCreateDate( formatter.format(date));
        //获取session
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        //获取用户名
        String user=(String) session.getAttribute("enterpriseAccountNumber");
        if(user==null){
            user = (String) session.getAttribute("adminAccountNumber");
        }else{
            user = (String) session.getAttribute("enterpriseAccountNumber");
        }
        sysLog.setUsername(user);
        //获取用户姓名
        String username=(String) session.getAttribute("enterpriseName");
        if(username==null){
            username = (String) session.getAttribute("adminName");
        }else{
            username = (String) session.getAttribute("enterpriseName");
        }
        sysLog.setName(username);
        //获取用户ip地址
        request = HttpContextUtils.getHttpServletRequest();
        sysLog.setIp(IPUtil.getIp(request));

        //调用service保存SysLog实体类到数据库
        sysLogService.save(sysLog);
    }

}
